package com.tangk.proxy;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Logger;

import com.tangk.tool.ITools;
import com.tangk.tool.author.core.Config;
import com.tangk.tool.util.ReflectUtils;

public  class DefaultProxHandler implements ITools{
	private static Logger logger = Logger.getLogger("main");
	ITools tools;
	public DefaultProxHandler(ITools tools){
		this.tools = tools;
	}
	@Override
	public Map<String, Object> doIt(Map<String, Object> params) {
		logger.info("代理开始！");
		List<ProxyMeta> filter = Config.getBeforFilter();
		if(doFilter(filter) == false){
			throw new RuntimeException();
		}
		
		Map<String, Object> rst = tools.doIt(params);
		
		filter = Config.getEndFilter();
		if(doFilter(filter) == false){
			throw new RuntimeException();
		}
		logger.info("代理结束");
		
		return rst;
	}

	private Boolean doFilter(List<ProxyMeta> filter) {
		Boolean b = null;
		for(ProxyMeta pMeta:filter){
			logger.info("开始代理:"+pMeta.getName());
			logger.info("代理方法:"+pMeta.getMethodName());
			b = ReflectUtils.invokeMehod(pMeta.getTarget(), pMeta.getMethodName(), true);
			if(b == false){
				return false;
			}
		}
		return true;
	}
	@Override
	public void end() {
		tools.end();
	}
	
}
